/**
 * GTMessageBar.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */

#import "GTMessageBar.h"
#import "UIKit+GTExtensions.h"

#define GTMESSAGEVIEW_HEIGHT 18.0

@interface GTMessageBar ()
- (UILabel*)createLabel;
@property(nonatomic,retain) UILabel *leftMessageLabel;
@property(nonatomic,retain) UILabel *rightMessageLabel;
@end


@implementation GTMessageBar
@synthesize leftMessageLabel;
@synthesize rightMessageLabel;

+ (CGFloat)height {
  return GTMESSAGEVIEW_HEIGHT;
}

- (id)initWithBackgroundColor:(UIColor *)aBackgroundColor textColor:(UIColor *)aTextColor font:(UIFont*)aFont {
  if ((self = [super initWithFrame:CGRectMake(0.0, 0.0, 320.0, GTMESSAGEVIEW_HEIGHT)])) {
    textColor = [aTextColor retain];
    font = [aFont retain];
    self.backgroundColor = aBackgroundColor;
  }
  return self;
}

- (void)dealloc {
  [textColor release];
  [font release];
  [leftMessageLabel release];
  [rightMessageLabel release];
  [super dealloc];
}

- (void)setMessage:(NSString*)aMessage {
  if (!leftMessageLabel) {
    self.leftMessageLabel = [self createLabel];
    leftMessageLabel.textAlignment = UITextAlignmentLeft;
    [self addSubview:leftMessageLabel];
  }
  [leftMessageLabel performSelectorOnMainThread:@selector(setText:) withObject:aMessage waitUntilDone:YES];
}

- (void)setLeftMessage:(NSString*)aLeftMessage rightMessage:(NSString*)aRightMessage {
  [self setMessage:aLeftMessage];
  if (!rightMessageLabel) {
    self.rightMessageLabel = [self createLabel];
    rightMessageLabel.textAlignment = UITextAlignmentRight;
    [self addSubview:rightMessageLabel];
  }
  [rightMessageLabel performSelectorOnMainThread:@selector(setText:) withObject:aRightMessage waitUntilDone:YES];
}

- (UILabel*)createLabel {
  CGSize size = [@"ABC" sizeWithFont:font];
  UILabel *aMessageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, size.height)];
  aMessageLabel.textColor = textColor;
  aMessageLabel.font = font;
  aMessageLabel.backgroundColor = [UIColor clearColor];
  return [aMessageLabel autorelease];
}

@end